package com.shanghaionstar.notifysetting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.notify.NotifyConfigRes;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.user.ChangeEmailOrPhoneFragment;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;

import static com.shanghaionstar.OnstarApplication.getApplication;
import static com.shanghaionstar.notifysetting.presenter.NotifyPresenter.IBType.CHANGE_FROM_NOTIFY;


/**
 * Created by Rengui Lu on 2017/4/24.
 */

public class SmsEmptyHelper {
    private Context context;
    private NotifyConfigRes mNotifyConfig;

    public SmsEmptyHelper(Context context, NotifyConfigRes mNotifyConfig) {
        this.context = context;
        this.mNotifyConfig = mNotifyConfig;
    }

    public void handleEmpty(TextView tv_tipNotifySms, final boolean isEmail) {
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(context instanceof SetANINotifyActivity) {
                    BusinessUtil.sendReportByClickType(getApplication(), FunctionNameFindId.TANSetting_MobilePhoneSetting.getStringValue());
                }else if(context instanceof  SetDANotifyActivity){
                    BusinessUtil.sendReportByClickType(getApplication(),isEmail? FunctionNameFindId.DASetting_EmailSetting.getStringValue():FunctionNameFindId.DASetting_MobilePhoneSetting.getStringValue());
                }else if(context instanceof  SetOVDNotifyActivity){
                    BusinessUtil.sendReportByClickType(getApplication(), FunctionNameFindId.OVDSetting_MobilePhoneSetting.getStringValue());
                }
                jumpToMore(mNotifyConfig, MoreActivity.class, isEmail);
            }
        };
        String emptyPhone = isEmail ? context.getString(R.string.notify_mail_empty) : context.getString(R.string.notify_sms_empty);
        tv_tipNotifySms.setHighlightColor(context.getResources().getColor(android.R.color.transparent));
        SpannableString spanInfo = new SpannableString(emptyPhone);
        int len = emptyPhone.length();
        spanInfo.setSpan(new NotifyClickSpan(clickListener, context), len
                - 4, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_tipNotifySms.setText(spanInfo);
        tv_tipNotifySms.setMovementMethod(LinkMovementMethod.getInstance());
    }


    public void jumpToMore(NotifyConfigRes notifyConfigRes, Class<?> clz, boolean isEmail) {
        Intent it = new Intent(context, clz);
        Bundle phoneBundle = new Bundle();
        phoneBundle.putSerializable(CHANGE_FROM_NOTIFY, notifyConfigRes);
        phoneBundle.putString("jumpValue", isEmail ? notifyConfigRes.getMail() : notifyConfigRes.getPhone());
        phoneBundle.putBoolean("isEmail", isEmail);
        phoneBundle.putString(FinalConstants.JUMP_FLAG, ChangeEmailOrPhoneFragment.class.getName());
        it.putExtras(phoneBundle);
        context.startActivity(it);
    }

    public void handleSms(TextView tv_tipNotifySms, int resId) {
        String phoneNum = mNotifyConfig.getPhone();
        if (TextUtils.isEmpty(phoneNum)) {
            handleEmpty(tv_tipNotifySms, false);
        } else {
            tv_tipNotifySms.setText(String.format(context.getString(resId), CommonUtil.replaceString(mNotifyConfig.getPhone(), false)));
        }
    }

    public void handleSms(TextView tv_tipNotifySms, String str) {
        String phoneNum = mNotifyConfig.getPhone();
        if (TextUtils.isEmpty(phoneNum)) {
            handleEmpty(tv_tipNotifySms, false);
        } else {
            tv_tipNotifySms.setText(str);
        }
    }

    public void handleEmail(TextView tv_tipNotifyMail, int resId) {
        String emailNum = mNotifyConfig.getMail();
        if (TextUtils.isEmpty(emailNum)) {
            handleEmpty(tv_tipNotifyMail, true);
        } else {
            tv_tipNotifyMail.setText(String.format(context.getString(resId), CommonUtil.replaceString(mNotifyConfig.getMail(), true)));
        }
    }

}
